﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace PeekService
{
    public partial class TourismInfoService
    {
        /// <summary>
        /// 增加一条记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(TourismInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into TourismInfo(");
            strSql.Append("Code,TypeCode,RegionCode,Tags,IsEspecially,Title,[Desc],Remark,State,CreateID,CreateTime,Sort,Hits,IsPublish,SubTitle,TicketDan,TicketWangFan,SpecialRecommand)");
            strSql.Append(" values (");
            strSql.Append("@Code,@TypeCode,@RegionCode,@Tags,@IsEspecially,@Title,@Desc,@Remark,@State,@CreateID,@CreateTime,@Sort,@Hits,@IsPublish,@SubTitle,@TicketDan,@TicketWangFan,@SpecialRecommand)");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36),
                    new SqlParameter("@TypeCode", SqlDbType.VarChar,36),
                    new SqlParameter("@RegionCode", SqlDbType.VarChar,36),
                    new SqlParameter("@Tags", SqlDbType.NVarChar,500),
					new SqlParameter("@IsEspecially", SqlDbType.Bit,1),
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@Desc", SqlDbType.NVarChar,4000),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
                    new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4),
                    new SqlParameter("@Hits", SqlDbType.Int,4),
                    new SqlParameter("@IsPublish",SqlDbType.NChar,1),
                    new SqlParameter("@SubTitle",SqlDbType.NVarChar,50),
                    new SqlParameter("@TicketDan",SqlDbType.NVarChar,500),
                    new SqlParameter("@TicketWangFan",SqlDbType.NVarChar,500),
                    new SqlParameter("@SpecialRecommand",SqlDbType.NVarChar,500)
                                        };
            parameters[0].Value = model.Code;
            parameters[1].Value = model.TypeCode;
            parameters[2].Value = model.RegionCode;
            parameters[3].Value = model.Tags;
            parameters[4].Value = model.IsEspecially;
            parameters[5].Value = model.Title;
            parameters[6].Value = model.Desc;
            parameters[7].Value = model.Remark;
            parameters[8].Value = model.State;
            parameters[9].Value = model.CreateID;
            parameters[10].Value = model.CreateTime;
            parameters[11].Value = model.Sort;
            parameters[12].Value = model.Hits;
            parameters[13].Value = model.IsPublish;
            parameters[14].Value = model.SubTitle;
            parameters[15].Value = model.TicketDan;
            parameters[16].Value = model.TicketWangFan;
            parameters[17].Value = model.SpecialRecommand;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TourismInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TourismInfo set ");
            strSql.Append("TypeCode=@TypeCode,");
            strSql.Append("RegionCode=@RegionCode,");
            strSql.Append("Tags=@Tags,");
            strSql.Append("IsEspecially=@IsEspecially,");
            strSql.Append("Title=@Title,");
            strSql.Append("[Desc]=@Desc,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("State=@State,");
            strSql.Append("CreateID=@CreateID,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("Hits=@Hits,");
            strSql.Append("IsPublish=@IsPublish,");
            strSql.Append("SubTitle=@SubTitle,");
            strSql.Append("TicketDan=@TicketDan,");
            strSql.Append("TicketWangFan=@TicketWangFan,");
            strSql.Append("SpecialRecommand=@SpecialRecommand");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
						new SqlParameter("@Code", SqlDbType.VarChar,36),
                    new SqlParameter("@TypeCode", SqlDbType.VarChar,36),
                    new SqlParameter("@RegionCode", SqlDbType.VarChar,36),
                    new SqlParameter("@Tags", SqlDbType.NVarChar,500),
					new SqlParameter("@IsEspecially", SqlDbType.Bit,1),
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@Desc", SqlDbType.NVarChar,4000),
					new SqlParameter("@Remark", SqlDbType.NVarChar,500),
                    new SqlParameter("@State", SqlDbType.Int,4),
					new SqlParameter("@CreateID", SqlDbType.VarChar,36),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Sort", SqlDbType.Int,4),
                    new SqlParameter("@Hits", SqlDbType.Int,4),
                    new SqlParameter("@IsPublish", SqlDbType.NChar,1),
                    new SqlParameter("@SubTitle", SqlDbType.NVarChar,50),
                    new SqlParameter("@TicketDan",SqlDbType.NVarChar,500),
                    new SqlParameter("@TicketWangFan",SqlDbType.NVarChar,500),
                    new SqlParameter("@SpecialRecommand",SqlDbType.NVarChar,500)
                                        };
            parameters[0].Value = model.Code;
            parameters[1].Value = model.TypeCode;
            parameters[2].Value = model.RegionCode;
            parameters[3].Value = model.Tags;
            parameters[4].Value = model.IsEspecially;
            parameters[5].Value = model.Title;
            parameters[6].Value = model.Desc;
            parameters[7].Value = model.Remark;
            parameters[8].Value = model.State;
            parameters[9].Value = model.CreateID;
            parameters[10].Value = model.CreateTime;
            parameters[11].Value = model.Sort;
            parameters[12].Value = model.Hits;
            parameters[13].Value = model.IsPublish;
            parameters[14].Value = model.SubTitle;
            parameters[15].Value = model.TicketDan;
            parameters[16].Value = model.TicketWangFan;
            parameters[17].Value = model.SpecialRecommand;
            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from TourismInfo ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;

            int rows = DBHelper.ExecuteNonQuery(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TourismInfo GetModel(string Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Code, TypeCode,RegionCode,Tags,IsEspecially,Title,[Desc],Remark,State,CreateID,CreateTime,Sort,Hits,IsPublish,SubTitle,TicketDan,TicketWangFan,SpecialRecommand from TourismInfo ");
            strSql.Append(" where Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,36)			};
            parameters[0].Value = Code;

            using (SqlDataReader dr = DBHelper.ExecuteReader(strSql.ToString(), parameters))
            {
                if (dr.Read())
                {
                    return DataRowToModel(dr);
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 获取所有分页
        /// </summary>
        /// <returns></returns>
        public List<TourismInfo> GetAll(int pageIndex, int pageSize, out int RecordCount)
        {
            string strsql = @"select Code,TypeCode,RegionCode,Tags,IsEspecially,Title,[Desc],Remark,State,CreateID,CreateTime,Sort,Hits,IsPublish,SubTitle,TicketDan,TicketWangFan,SpecialRecommand from TourismInfo order by sort desc";
            List<TourismInfo> list = new List<TourismInfo>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strsql, pageIndex, pageSize, out RecordCount))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }

        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        public List<TourismInfo> GetAll()
        {
            string strsql = @"select Code,TypeCode,RegionCode,Tags,IsEspecially,Title,[Desc],Remark,State,CreateID,CreateTime,Sort,Hits,IsPublish,SubTitle,TicketDan,TicketWangFan,SpecialRecommand from TourismInfo order by sort desc";
            List<TourismInfo> list = new List<TourismInfo>();
            using (SqlDataReader dr = DBHelper.ExecuteReader(strsql))
            {
                while (dr.Read())
                {
                    list.Add(DataRowToModel(dr));
                }
            }
            return list;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TourismInfo DataRowToModel(SqlDataReader reader)
        {
            TourismInfo model = new TourismInfo();
            if (reader != null)
            {
                if (reader["Code"] != null)
                {
                    model.Code = reader["Code"].ToString();
                }
                if (reader["TypeCode"] != null)
                {
                    model.TypeCode = reader["TypeCode"].ToString();
                }
                if (reader["RegionCode"] != null)
                {
                    model.RegionCode = reader["RegionCode"].ToString();
                }
                if (reader["Tags"] != null)
                {
                    model.Tags = reader["Tags"].ToString();
                }
                if (reader["IsEspecially"] != null)
                {
                    model.IsEspecially = (bool)reader["IsEspecially"];
                }
                if (reader["Title"] != null)
                {
                    model.Title = reader["Title"].ToString();
                }
                if (reader["Desc"] != null)
                {
                    model.Desc = reader["Desc"].ToString();
                }
                if (reader["Remark"] != null)
                {
                    model.Remark = reader["Remark"].ToString();
                }
                if (reader["State"] != null && reader["State"].ToString() != "")
                {
                    model.State = int.Parse(reader["State"].ToString());
                }
                if (reader["CreateID"] != null)
                {
                    model.CreateID = reader["CreateID"].ToString();
                }
                if (reader["CreateTime"] != null && reader["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(reader["CreateTime"].ToString());
                }
                if (reader["Sort"] != null && reader["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(reader["Sort"].ToString());
                }
                if (reader["Hits"] != null && reader["Hits"].ToString() != "")
                {
                    model.Hits = int.Parse(reader["Hits"].ToString());
                }
                if (reader["TicketDan"] != null && reader["TicketDan"].ToString() != "")
                {
                    model.TicketDan = reader["TicketDan"].ToString();
                }
                if (reader["TicketWangFan"] != null && reader["TicketWangFan"].ToString() != "")
                {
                    model.TicketWangFan = reader["TicketWangFan"].ToString();
                }
                if (reader["IsPublish"] != DBNull.Value)
                {
                    model.IsPublish = reader["IsPublish"].ToString();
                }
                if (reader["SpecialRecommand"] != DBNull.Value)
                {
                    model.SpecialRecommand = reader["SpecialRecommand"].ToString();
                }
            }
            return model;
        }

    }
}
